package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.dbinfohistory.DbInfoHistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.dbinfohistory.DbInfoHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.dbinfohistory.DbInfoHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dbinfohistory.DbInfoHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dbinfohistory.DbInfoHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.dbinfohistory.DbInfoHistoryVo;
import com.bwda.dsrs.filecenter.service.DbInfoHistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/dbinfohistory")
@Api(value = "/dbinfohistory", tags = "-- 数据库信息历史表")
public class DbInfoHistoryController extends BaseController {

	@Autowired
	private DbInfoHistoryService dbInfoHistoryService;

	@ApiOperation(value = "查询-- 数据库信息历史表",notes = "查询-- 数据库信息历史表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbInfoHistoryVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer dbInfoHistoryId) throws BusinessServiceException {
		DbInfoHistoryPo po = dbInfoHistoryService.queryWithValid(dbInfoHistoryId);
		DbInfoHistoryVo vo = CopyUtil.transfer(po, DbInfoHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询-- 数据库信息历史表数量",notes = "查询-- 数据库信息历史表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DbInfoHistoryQueryForm form) throws BusinessServiceException {
		DbInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		int count = dbInfoHistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询-- 数据库信息历史表列表",notes = "查询-- 数据库信息历史表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DbInfoHistoryVo>> queryList(@ModelAttribute@Valid DbInfoHistoryQueryForm form) throws BusinessServiceException {
		DbInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		List<DbInfoHistoryPo> poList = dbInfoHistoryService.queryList(condition);
		List<DbInfoHistoryVo> voList = CopyUtil.transfer(poList, DbInfoHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询-- 数据库信息历史表列表(带分页)",notes = "查询-- 数据库信息历史表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DbInfoHistoryVo>> queryPageList(@ModelAttribute@Valid DbInfoHistoryQueryForm form) throws BusinessServiceException {
		DbInfoHistoryCondition condition = this.getConditionByQueryForm(form);
		List<DbInfoHistoryVo> voList = new ArrayList<>();
		int count = dbInfoHistoryService.queryCount(condition);
		if (count > 0) {
			List<DbInfoHistoryPo> poList = dbInfoHistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, DbInfoHistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增-- 数据库信息历史表",notes = "新增-- 数据库信息历史表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DbInfoHistoryVo> add(@ModelAttribute@Valid DbInfoHistoryCreateForm form) throws BusinessServiceException {
		DbInfoHistoryPo po = CopyUtil.transfer(form, DbInfoHistoryPo.class);
//		po.setDbInfoHistoryId(RandomUtils.nextInt());
		dbInfoHistoryService.insert(po);
		DbInfoHistoryVo vo = CopyUtil.transfer(po, DbInfoHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改-- 数据库信息历史表",notes = "修改-- 数据库信息历史表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DbInfoHistoryUpdateForm form) throws BusinessServiceException {
		DbInfoHistoryPo po = CopyUtil.transfer(form, DbInfoHistoryPo.class);
		dbInfoHistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除-- 数据库信息历史表",notes = "删除-- 数据库信息历史表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer dbInfoHistoryId) throws BusinessServiceException {
		dbInfoHistoryService.delete(dbInfoHistoryId);
		return getSuccessResult();
	}

	/**
	 * DbInfoHistoryQueryForm转换为DbInfoHistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private DbInfoHistoryCondition getConditionByQueryForm(DbInfoHistoryQueryForm form) {
		DbInfoHistoryCondition condition = CopyUtil.transfer(form, DbInfoHistoryCondition.class);
		return condition;
	}

}